package jwei.apps;

import jwei.apps.db.StyleDb;
import jwei.apps.helpers.ImageService;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;

public class ProductDetailsActivity extends TabActivity {

    private TabHost mTabHost;

    private TabWidget mTabWidget;

    private long productId, styleId;

    private ImageView prodImg;

    private TextView productText, brandText, priceText;

    private String productUrl;

    private Button productBrowser, productQueue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.product_details_layout);
        Resources res = getResources();

        Intent i = getIntent();
        productId = i.getLongExtra("productId", 0);
        styleId = i.getLongExtra("styleId", 0);
        String price = i.getStringExtra("price");
        String productName = i.getStringExtra("productName");
        String brandName = i.getStringExtra("brandName");
        String imageUrl = i.getStringExtra("imageUrl");
        productUrl = i.getStringExtra("productUrl");

        productText = (TextView) findViewById(R.id.product_name);
        brandText = (TextView) findViewById(R.id.brand_name);
        priceText = (TextView) findViewById(R.id.product_price);
        prodImg = (ImageView) findViewById(R.id.product_image);

        productBrowser = (Button) findViewById(R.id.product_browser);
        productQueue = (Button) findViewById(R.id.add_product_to_queue);

        productBrowser.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(productUrl));
                startActivity(i);
            }

        });

        StyleDb sdb = StyleDb.getInstance();
        int status = sdb.getStyleQueueStatus(getContentResolver(), productId);
        if (status > 0) {
            productQueue.setBackgroundResource(R.drawable.trash_can_selector);
        }

        productQueue.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                StyleDb sdb = StyleDb.getInstance();
                int curStatus = sdb.getStyleQueueStatus(getContentResolver(), productId);
                if (curStatus > 0) {
                    sdb.updateStyleQueue(getContentResolver(), productId, 0);
                    productQueue.setBackgroundResource(R.drawable.shopping_bag_selector);
                    Toast t = Toast.makeText(ProductDetailsActivity.this, "Removed product from your shopping cart.",
                            Toast.LENGTH_SHORT);
                    t.show();
                } else {
                    boolean success = sdb.updateStyleQueue(getContentResolver(), productId, 1);
                    if (success) {
                        productQueue.setBackgroundResource(R.drawable.trash_can_selector);
                        Toast t = Toast.makeText(ProductDetailsActivity.this, "Added product to your shopping cart!",
                                Toast.LENGTH_SHORT);
                        t.show();
                    } else {
                        Toast t = Toast.makeText(ProductDetailsActivity.this,
                                "Uh oh! Couldn't add this product to your shopping cart.", Toast.LENGTH_SHORT);
                        t.show();
                    }
                }
            }

        });

        new LoadImageTask().execute(imageUrl);
        productText.setText(productName);
        brandText.setText(brandName);
        priceText.setText(price);

        mTabHost = getTabHost();
        mTabWidget = getTabWidget();

        i = new Intent().setClass(this, ProductSimilarActivity.class);
        i.putExtra("styleId", styleId);
        TabSpec detailsTab = mTabHost.newTabSpec("similar").setIndicator("Similar Products",
                res.getDrawable(android.R.drawable.star_big_on)).setContent(i);
        mTabHost.addTab(detailsTab);

        i = new Intent().setClass(this, ProductReviewsActivity.class);
        i.putExtra("productId", productId);
        TabSpec reviewsTab = mTabHost.newTabSpec("reviews").setIndicator("Reviews",
                res.getDrawable(android.R.drawable.sym_action_chat)).setContent(i);
        mTabHost.addTab(reviewsTab);

        final TextView tv = (TextView) mTabWidget.getChildAt(0).findViewById(android.R.id.title);
        tv.setTextColor(Color.BLACK);

        final TextView tv2 = (TextView) mTabWidget.getChildAt(1).findViewById(android.R.id.title);
        tv2.setTextColor(Color.BLACK);
    }

    private class LoadImageTask extends AsyncTask<String, String, Bitmap> {

        @Override
        protected void onPreExecute() {
        }

        // param[0] is image url
        protected Bitmap doInBackground(String... param) {
            // get bitmap from url
            Bitmap bm = ImageService.getBitmapFromURL(param[0]);
            return bm;
        }

        protected void onProgressUpdate(String... progress) {
        }

        protected void onPostExecute(Bitmap bm) {
            if (bm != null) {
                prodImg.setImageBitmap(bm);
            }
        }
    }

}
